//
//  HYAgreementViewController.m
//  Booking
//
//  Created by Maybe_文仔 on 2018/9/21.
//  Copyright © 2018年 Duntech. All rights reserved.
//

#import "HYAgreementViewController.h"

@interface HYAgreementViewController ()

@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UITextView *agreementContent;
@property (nonatomic, strong) WKWebView *webView;

@end

@implementation HYAgreementViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setupViews];
}

- (void)setupViews{
    
    self.view.backgroundColor = KAPP_WHITE_COLOR;
    
    [self.view addSubview:self.backBtn];
    [self.view addSubview:self.webView];
   
    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(64);
    }];
}

#pragma mark -- lazyload

-(UIButton *)backBtn{
    
    if (!_backBtn) {
        _backBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, 35, 25, 25)];
        [_backBtn setImage:[UIImage imageNamed:@"back"] forState:(UIControlStateNormal)];
        kWeakSelf(weakSelf);
        [_backBtn addBlockForControlEvents:(UIControlEventTouchUpInside) block:^(id  _Nonnull sender) {
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }];
    }
    return _backBtn;
}

-(UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc]init];
        _titleLab.text = @"TERMS OF USE";
        _titleLab.textColor = [UIColor redColor];
        _titleLab.font = KFitBoldFont(22);
    }
    return _titleLab;
}

- (WKWebView *)webView{
    if (!_webView) {
        
        NSString *jsStr = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
        WKUserScript *userScript = [[WKUserScript alloc] initWithSource:jsStr injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *userContentController = [[WKUserContentController alloc] init];
        [userContentController addUserScript:userScript];
        //创建WKWebView的配置对象
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        configuration.userContentController = userContentController;
        
        _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];;
        _webView.backgroundColor = [UIColor whiteColor];
        
        NSString *path =  [[NSBundle mainBundle] pathForResource:@"userProtocol" ofType:@"html"];
        NSURL *protocolUrl = [NSURL fileURLWithPath:path];
        [_webView loadRequest:[NSURLRequest requestWithURL:protocolUrl]];
        
        //
        //        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        //        //设置configuration对象的preferences属性的信息
        //        WKPreferences *preference = [[WKPreferences alloc] init];
        //        configuration.preferences = preference;
        //        //是否允许与JS交互，默认YES
        //        preference.javaScriptEnabled = YES;
        //        //通过JS与WebView内容交互
        //        configuration.userContentController = [[WKUserContentController alloc] init];
        //        [configuration.userContentController addScriptMessageHandler:self name:@"callback"];
        
    }
    return _webView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
